<template>
  <div class="avatar-selector">
    <div
      v-for="(item, index) in avatars"
      :key="index"
      :class="{ 'avatar-selector__item': true, 'active': selectIndex === index }"
      @click="selectAvatar(item)"
    >
      <Avatar :src="item" />
    </div>
  </div>
</template>

<script>
// 头像集合
const AVATARS = [
  "https://picture-1256514897.file.myqcloud.com/book/images/avatar1.png",
  "https://picture-1256514897.file.myqcloud.com/book/images/avatar2.png",
  "https://picture-1256514897.file.myqcloud.com/book/images/avatar3.png",
  "https://picture-1256514897.file.myqcloud.com/book/images/avatar4.png",
  "https://picture-1256514897.file.myqcloud.com/book/images/avatar5.png",
  "https://picture-1256514897.file.myqcloud.com/book/images/avatar6.png"
];

export default {
  name: "m-avatar-selector",

  props: {
    value: String
  },

  data() {
    return {
      avatars: AVATARS
    };
  },

  computed: {
    selectIndex() {
      return this.avatars.indexOf(this.value);
    }
  },

  methods: {
    selectAvatar(url) {
      this.$emit("update:value", url);
    }
  }
};
</script>

<style lang="scss" scoped>
.avatar-selector {
  &__item {
    margin-right: 10px;
    display: inline-block;
    border-radius: 16px;

    &.active .ivu-avatar {
      border: 2px solid green;
      position: relative;
      &::before {
        content: " ";
        position: absolute;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.6);
      }

      &::after {
        content: "✓";
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        color: #fff;
        font-size: 14px;
      }
    }
  }
}
</style>
